package com.example.springdemo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AspectJByAno {

    @Pointcut("execution(* com.example.springdemo.aop.*.*(..))")
    public void pointCutByAno() {

    }
    @Before("pointCutByAno()")
    public void myBefore(JoinPoint joinPoint) {
        System.out.println("基于注解的前置通知");
    }

    @Around("pointCutByAno()")
    public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("基于注解的环绕通知开始");
        Object obj = joinPoint.proceed();
        System.out.println("基于注解的环绕通知结束");
        return obj;
    }
    @AfterReturning("pointCutByAno()")
    public void myAfterReturn(JoinPoint joinPoint) {
        System.out.println("基于注解的后置返回通知");
    }

    @After("pointCutByAno()")
    public void myAfterFinal(JoinPoint joinPoint) {
        System.out.println("基于注解的后置最终通知");
    }
    @AfterThrowing(value = "pointCutByAno()",throwing = "e")
    public void myException(JoinPoint joinPoint,Throwable e) {
        System.out.println("基于注解的异常通知");
        System.out.println("异常信息：" + e.getMessage());
    }
}
